package com.yuanqi.java.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuanqi.java.dto.PurchaseStatisticsOverviewVO;
import com.yuanqi.java.dto.PurchaseStatisticsQueryDTO;
import com.yuanqi.java.dto.PurchaseStatisticsVO;
import com.yuanqi.java.result.Result;
import com.yuanqi.java.service.IPurchaseStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 采购统计前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-04
 */
@RestController
@RequestMapping("/purchase-statistics")
public class PurchaseStatisticsController {

    @Autowired
    private IPurchaseStatisticsService purchaseStatisticsService;

    /**
     * 分页查询采购统计
     */
    @GetMapping("/page")
    public Result<Map<String, Object>> page(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String supplierInfo,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String specification,
            @RequestParam(required = false) String purchaseDate
    ) {
        Page<PurchaseStatisticsVO> page = new Page<>(current, size);
        
        PurchaseStatisticsQueryDTO queryDTO = new PurchaseStatisticsQueryDTO();
        queryDTO.setSupplierInfo(supplierInfo);
        queryDTO.setProductName(productName);
        queryDTO.setSpecification(specification);
        queryDTO.setPurchaseDate(purchaseDate);
        
        Page<PurchaseStatisticsVO> result = purchaseStatisticsService.pageQuery(page, queryDTO);
        
        Map<String, Object> data = new HashMap<>();
        data.put("records", result.getRecords());
        data.put("total", result.getTotal());
        data.put("current", result.getCurrent());
        data.put("size", result.getSize());
        
        return Result.success(data);
    }

    /**
     * 获取采购统计概览
     */
    @GetMapping("/overview")
    public Result<PurchaseStatisticsOverviewVO> overview(
            @RequestParam(required = false) String supplierInfo,
            @RequestParam(required = false) String productName,
            @RequestParam(required = false) String specification,
            @RequestParam(required = false) String purchaseDate
    ) {
        PurchaseStatisticsQueryDTO queryDTO = new PurchaseStatisticsQueryDTO();
        queryDTO.setSupplierInfo(supplierInfo);
        queryDTO.setProductName(productName);
        queryDTO.setSpecification(specification);
        queryDTO.setPurchaseDate(purchaseDate);
        
        PurchaseStatisticsOverviewVO overview = purchaseStatisticsService.getOverview(queryDTO);
        return Result.success(overview);
    }
} 